//
//  ZBMoBanEightLineView.m
//  MoBan
//
//  Created by apple on 16/7/16.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZBMoBanEightLineView.h"
//#import "ZBMoBanEightButten.h"
@interface ZBMoBanEightLineView()
@property(nonatomic,strong)NSMutableArray *array;
@end

@implementation ZBMoBanEightLineView
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;

}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self AddUI];
    }
    return self;
}
-(void)AddUI
{
    self.backgroundColor = [UIColor redColor];
    
    NSMutableArray *array = [self.deleget ZBLineViewIteamCount];
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.array removeAllObjects];

    for (NSInteger i=0; i<array.count; i++) {
        //创建butten
        ZBMoBanEightButten *butten = [[ZBMoBanEightButten alloc]init];
        butten.tag = i;
        [butten addTarget:self action:@selector(ZBLineBtnClic:) forControlEvents:UIControlEventTouchUpInside];
        //取出传入的数组的模型
        ZBButtenDataSouce * buttenmodel = array[i];
        butten.lable.text =buttenmodel.namecount;
        butten.detailable.text = buttenmodel.name;
        
        //创建lable
        UILabel *lable = [[UILabel alloc]init];
        lable.backgroundColor = [UIColor grayColor];
        
        //让模型的指针指向butten和lable方便存储
        ZBMoBanEightArrayModel *model = [[ZBMoBanEightArrayModel alloc]init];
        model.mobanbutten = butten;
        model.lable = lable;
        [self.array addObject:model];
        [self addSubview:butten];
        [self addSubview:lable];
        
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat ButtenWideth = self.width/self.array.count;
    
    for (NSInteger i=0; i<self.array.count; i++) {
        
        ZBMoBanEightArrayModel *model = self.array[i];
        
        model.mobanbutten.frame = CGRectMake(ButtenWideth*i, 0, ButtenWideth, self.height);
        if (i<self.array.count-1) {
            model.lable.frame  = CGRectMake(ButtenWideth*(i+1), 5*Kscr, 1, self.height-10*Kscr);
        }
        
    }
    

}
-(void)reloadData
{
    [self AddUI];
}
-(void)ZBLineBtnClic:(ZBMoBanEightButten *)butten
{

    if ([self.deleget respondsToSelector:@selector(ZBLineView:didSelectindex:)]) {
        [self.deleget ZBLineView:self didSelectindex:butten.tag];
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
